package com.chinackts.auth.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinackts.auth.model.RdTime;
import com.chinackts.auth.repository.redis.RdTimeRepository;
import com.chinackts.common.okhttp.OkHttpRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Lianhong
 * @since 2021/10/26
 * 请求头部rdtime，发送请求前，将rdtime添加至请求头。
 */
@Service
public class RdTimeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RdTimeService.class);
    private static final String RTX_RDTIME_URL = "https://i.chinaport.gov.cn/rtxwebserver/sw/rtx/commonQuery/getSession";
    private static final String FEC_RDTIME_URL = "https://i.chinaport.gov.cn/fecwebserver/session/getRdtime";

    private static final String SPL_RDTIME_URL = "https://swapp.singlewindow.cn/splserver/sw/spl/para/getUserinfo";


    @Resource
    private RdTimeRepository rdTimeRepository;
    @Resource(name = "proxyOkHttpRequest")
    private OkHttpRequest request;


    /**
     * 请求获取 rtx rdtime（报关单），并存储
     */
    public void rtxRdTimeRequestAndStore(String id) {
        boolean exists = rdTimeRepository.exists(id, RdTime.Rtx);
        if (exists) return;
        String result = request.post(RTX_RDTIME_URL, StringUtils.EMPTY);
        if (StringUtils.isBlank(result)) result = request.post(RTX_RDTIME_URL, StringUtils.EMPTY);
        LOGGER.info("Rtx rdtime: {}", result);
        if (StringUtils.isBlank(result)) return;
        String rtxRdTime = JSON.parseObject(result).getString("rdtime");
        rdTimeRepository.saveRdTime(id, rtxRdTime, RdTime.Rtx);
    }

    /**
     * 请求获取 fec rdtime（收付汇），并存储
     */
    public void fecRdTimeRequestAndStore(String id) {
        boolean exists = rdTimeRepository.exists(id, RdTime.Fec);
        if (exists) return;
        String response = request.post(FEC_RDTIME_URL, StringUtils.EMPTY);
        if (StringUtils.isBlank(response)) response = request.post(FEC_RDTIME_URL, StringUtils.EMPTY);
        LOGGER.info("Fec rdtime: {}", response);
        if (StringUtils.isBlank(response)) return;
        String rdTime = JSON.parseObject(response).getString("userrdtime");
        rdTimeRepository.saveRdTime(id, rdTime, RdTime.Fec);
    }

    /**
     * 请求获取 spl rdtime（税费单），并存储
     */
    public void splRdTimeRequestAndStore(String id) {
        boolean exists = rdTimeRepository.exists(id, RdTime.Spl);
        if (exists) return;
        String result = request.post(SPL_RDTIME_URL, StringUtils.EMPTY);
        if (StringUtils.isBlank(result)) result = request.post(SPL_RDTIME_URL, StringUtils.EMPTY);
        JSONObject userInfoJson = JSON.parseObject(result);
        LOGGER.info("Spl rdtime: {}", userInfoJson);
        String rdTime = userInfoJson
                .getJSONObject("unSafeBusinessData")

                .getJSONObject("data")
                .getString("rdtime");
        rdTimeRepository.saveRdTime(id, rdTime, RdTime.Spl);
    }
}
